package com.java01.lang.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class InputStreamReaderSnippet {

	public static void main(String[] args) {
		File file = new File("D:/readme.txt");
		InputStream is = null;
		InputStreamReader isr = null;

		try {
			is = new FileInputStream(file);
			//specifiy an encoding for inputstream data
			isr = new InputStreamReader(is,"UTF-8");

			char[] buffer = new char[1024];
			int length;
			while ((length=isr.read(buffer)) != -1) {
				System.out.println(">>" + String.valueOf(buffer,0,length));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(isr!=null){
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
